import 'package:chop/Model/order_entity.dart';
import 'package:chop/common/DateUtils.dart';
import 'package:chop/common/DioUtils.dart';
import 'package:chop/view/order/order_info.dart';
import 'package:chop/view/order/order_unpaid.dart';
import 'package:chop/widgets/LoginOutWidget.dart';
import 'package:chop/widgets/TextSizeWidget.dart';
import 'package:chop/widgets/ToastWidget.dart';
import 'package:chop/widgets/loadingDialog.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:get/get.dart';

import 'order_unfinished.dart';

Widget getOrderItem(context,
    {EasyRefreshController easyRefreshController, orderData data, int type}) {
  return GestureDetector(
    child: Container(
      width: double.infinity,
//    padding: EdgeInsets.all(5),
      margin: EdgeInsets.fromLTRB(10, 5, 10, 5),
      decoration: BoxDecoration(
//        border: z,
          borderRadius: BorderRadius.all(Radius.circular(10)),
          color: Colors.white),
      child: Row(
        children: <Widget>[
          Expanded(
            child: Column(
              children: <Widget>[
                getTitle(context, easyRefreshController, data),
                Container(
                  width: double.infinity,
                  padding: EdgeInsets.only(left: 10, bottom: 5),
                  child: Row(
                    children: <Widget>[
                      Icon(
                        Icons.access_time,
                        size: 15,
                      ),
                      FixedSizeText(
                        "  ${DateUtils.getStfDate(data.createTime)}",
                        textAlign: TextAlign.left,
                        overflow: TextOverflow.ellipsis,
                        style: TextStyle(),
                      )
                    ],
                  ),
                ),
                Container(
                  width: double.infinity,
                  padding: EdgeInsets.only(left: 10, bottom: 5),
                  child: Row(
                    children: <Widget>[
                      Icon(
                        Icons.subdirectory_arrow_right,
                        size: 15,
                        color: Colors.greenAccent,
                      ),
                      Container(
                        width: 5,
                      ),
                      Expanded(
                        child: FixedSizeText(
                          "${data.begin}",
                          textAlign: TextAlign.left,
                          maxLines: 4,
//                        overflow: TextOverflow.ellipsis,
                          style: TextStyle(),
                        ),
                      )
                    ],
                  ),
                ),
                Container(
                  width: double.infinity,
                  padding: EdgeInsets.only(left: 10, bottom: 5),
                  child: Row(
                    children: <Widget>[
                      Icon(
                        Icons.subdirectory_arrow_left,
                        size: 15,
                        color: Colors.redAccent,
                      ),
                      Container(
                        width: 5,
                      ),
                      Expanded(
                        child: FixedSizeText(
                          "${data.end}",
                          textAlign: TextAlign.left,
                          overflow: TextOverflow.ellipsis,
                          style: TextStyle(),
                        ),
                      )
                    ],
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    ),
    onTap: () {
      switch (type) {
        case 0:
          print("进行中订单详情");
          Get.to(MyOrderInfo(uuid: data.uuid,));
          break;
        case 1:
          print("已完成订单详情");
          Get.to(MyOrderInfo(uuid: data.uuid,));
          break;
        case 2:
          print("待支付订单");
          Get.to(OrderUnpaid(
            uuid: data.uuid,
          ));
          break;
        case 3:
          print("待接单");
          Get.to(MyOrderInfo(uuid: data.uuid,));
          break;
      }
    },
  );
}

Widget getTitle(
    context, EasyRefreshController easyRefreshController, orderData data) {
  if (data.status == "2" || data.status == "0") {
    return Row(
      children: <Widget>[
        Expanded(
          child: Container(
            padding: EdgeInsets.all(10),
            child: FixedSizeText(
              getOrderType(data.orderType),
              textAlign: TextAlign.left,
              style: TextStyle(fontSize: 18),
            ),
          ),
        ),
        Expanded(
            child: GestureDetector(
          onTap: () {
            LoginOut(context, text: "取消订单", click: () {
              print("取消订单");
              showDialog<Null>(
                context: context, //BuildContext对象
                barrierDismissible: false,
                builder: (BuildContext context) {
                  return LoadingDialog(
                    //调用对话框
                    text: 'Loading...',
                  );
                },
              );
              DioUtils.delOrder(data.uuid).then((Map m){
                easyRefreshController.callRefresh();
                Get.back();
                Get.back();
              });

            }, callback: () {
              Get.back();
            });
          },
          child: Container(
              alignment: Alignment.topRight,
              padding: EdgeInsets.all(10),
              child: Icon(
                Icons.close,
                color: Colors.redAccent,
              )),
        ))
      ],
    );
  } else if(data.status == "3"){
    return Row(
      children: <Widget>[
        Expanded(
          child: Container(
            padding: EdgeInsets.all(10),
            child: FixedSizeText(
              getOrderType(data.orderType),
              textAlign: TextAlign.left,
              style: TextStyle(fontSize: 18),
            ),
          ),
        ),
        Expanded(
            child: GestureDetector(
              onTap: () {
                LoginOut(context, text: "取消订单", click: () {
                  print("取消订单");
                  showDialog<Null>(
                    context: context, //BuildContext对象
                    barrierDismissible: false,
                    builder: (BuildContext context) {
                      return LoadingDialog(
                        //调用对话框
                        text: 'Loading...',
                      );
                    },
                  );
                  DioUtils.delOrder(data.uuid).then((Map m){
                    easyRefreshController.callRefresh();
                    Toast.toast(context,msg: "取消成功");
                    Get.back();
                    Get.back();
                  });

                }, callback: () {
                  Get.back();
                });
              },
              child: Container(
                  alignment: Alignment.topRight,
                  padding: EdgeInsets.all(10),
                  child: Icon(
                    Icons.close,
                    color: Colors.redAccent,
                  )),
            ))
      ],
    );
  } else {
    return Container();
  }
}

String getOrderType(String type) {
  print("type:----------$type");
  switch (type) {
    case "0":
      return "帮我送";
    case "1":
      return "帮我取";
    case "2":
      return "帮我买";
  }
}
